<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\controller\Admin;
use path\SystemInfoWindows;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Session;
use think\Request;

class Index extends Admin
{
    protected $model = false;
    public function index()
    {
        $admin_menu = Cache::get('admin_menu'.Session::get('login')['id']);
        // pr($admin_menu);
        // die;
        if(empty($admin_menu)){
            return redirect((string)url('user/login'));
        }
        $data = [];
        $this->assign('left_menu',$admin_menu[0]['p_menu']);
        return view();
    }

    public function home()
    {
        $UserIndex_model = app('app\admin\model\UserIndex');
        $Business_model = app('app\admin\model\Business');
        $Script_model = app('app\admin\model\Script');
        $OrderFleet_model = app('app\admin\model\OrderFleet');
        $OrderMember_model = app('app\admin\model\OrderMember');
        $data['user']['count'] = $UserIndex_model->count('id');
        $data['business']['count'] = $Business_model->count('id');
        $data['script']['count'] = $Script_model->count('id');
        $data['script']['pt'] = $Script_model->where('type',1)->count('id');
        $data['script']['md'] = $Script_model->where('type',2)->count('id');
        $data['order_fleet']['count'] = $OrderFleet_model->count('id');
        $data['order_fleet']['wc'] = $OrderFleet_model->where('status',4)->count('id');
        $data['order_member']['wc'] = $OrderMember_model->where('wc',1)->count('id');

        $this->assign('setting_data', $data);
        $data = [];
        //环境信息获取
        $data['path']['php_version'] = PHP_VERSION;
        if (@ini_get('file_uploads')) {
            $data['path']['upload_max_filesize'] = ini_get('upload_max_filesize');
        } else {
            $data['path']['upload_max_filesize'] = '禁止上传';
        }
        $data['path']['php_os'] = PHP_OS;
        $softArr = explode('/', $_SERVER["SERVER_SOFTWARE"]);
        $data['path']['server_software'] = array_shift($softArr);
        $data['path']['server_name'] = gethostbyname($_SERVER['SERVER_NAME']);
        $rslt = Db::query('SELECT VERSION() AS `version`');
        $data['path']['mysql_version'] = $rslt[0]['version'];
        if (extension_loaded('curl')) {
            $data['path']['curl_extension'] = 'YES';
        } else {
            $data['path']['curl_extension'] = 'NO';
        }

        if (extension_loaded('MBstring')) {
            $data['path']['mbstring_extension'] = 'YES';
        } else {
            $data['path']['mbstring_extension'] = 'NO';
        }

        if (extension_loaded('pdo')) {
            $data['path']['pdo_extension'] = 'YES';
        } else {
            $data['path']['pdo_extension'] = 'NO';
        }
        if (extension_loaded('redis')) {
            $data['path']['redis_extension'] = 'YES';
        } else {
            $data['path']['redis_extension'] = 'NO';
        }
        $data['path']['max_execution_time'] = ini_get('max_execution_time') . 'S';
        if ($this->request->isAjax()) {
            //实时更新CPU，内存使用率
            $windos = SystemInfoWindows::class;
            $windos = new $windos;
            $cpu = $windos->getCpuUsage();
            $memory = $windos->getMemoryUsage();
            $ram = $memory['FreePhysicalMemory'] / $memory['TotalVisibleMemorySize'];
            $ram = $ram * 100;
            $ram = intval($ram);
            return datasuccess(['cpu' => $cpu, 'ram' => $ram]);
        }
        $this->assign('data', $data);
        return view();
    }
    public function path()
    {
        //今日访问趋势
        $user_index_log_model = app('app\admin\model\UserIndexLog');
        $user_index_log_list = $user_index_log_model->where('create_time','>=',date('Y-m-d'))->select();
        $time_jd = [
            '00:00','01:00','02:00','03:00','04:00','05:00','06:00','07:00','08:00','09:00','10:00','11:00',
            '12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00'
        ];
        $time_count = [
            0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,
        ];
        if(!empty($user_index_log_list) && !empty($user_index_log_list->toArray()))
        {
            $user_index_log_list = $user_index_log_list->toArray();
            foreach ($user_index_log_list as $v){
                $v = substr($v['create_time'],11,2);
                foreach ($time_jd as $kk=>$vv){
                    if($vv == '00:00' && $v > '23:00'){
                        $time_count[0]++;
                        break;
                    }
                    if($vv > $v){
                        $time_count[$kk]++;
                        break;
                    }
                }
            }
        }
        //浏览器统计
        $Browser_model = app('app\admin\model\Browser');
        $Browser_list = $Browser_model->select()->toArray();
        $arr = [];
        $arr_s = [];
        foreach ($Browser_list as $k => $v) {
            $arr[$k]['value'] = $v['num'];
            $arr[$k]['name'] = $v['name'];
            $arr_s[] = $v['name'];
        }
        //注册统计
        $zc_count = Db::query("SELECT DATE(create_time) createtime,COUNT(*) FROM ysh_user_index_log WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= DATE(create_time) GROUP BY createtime");

        $zc_time = [];
        $zc_num = [];
        foreach ($zc_count as $k=>$v){
            if($k >= 7){
                break;
            }
            $zc_time[] = $v['createtime'];
            $zc_num[] = $v['COUNT(*)'];
        }
        $data = [
            //今日流量趋势
            [
                'title' => [
                    'text' => '今日区间流量',
                    'x' => 'center',
                    'textStyle' => [
                        'fontSize' => 14
                    ]
                ],
                'tooltip' => [
                    'trigger' => 'axis'
                ],
                'legend' => [
                    'data' => ['', '']
                ],
                'xAxis' => [
                    [
                        'type' => 'category',
                        'boundaryGap' => false,
                        'data' => ['01:00','02:00','03:00','04:00','05:00','06:00', '06:30', '07:00', '07:30', '08:00', '08:30', '09:00', '09:30', '10:00', '11:30', '12:00', '12:30', '13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30', '18:00', '18:30', '19:00', '19:30', '20:00', '20:30', '21:00', '21:30', '22:00', '22:30', '23:00', '23:30']
                    ]
                ],
                'yAxis' => [
                    [
                        'type' => 'value'
                    ]
                ],
                'series' => [
                    [
                        'name' => 'PV',
                        'type' => 'line',
                        'smooth' => true,
                        'itemStyle' => ['normal' => ['areaStyle' => ['type' => 'default']]],
                        'data' => [111,111,111,111,111,111, 222, 333, 444, 555, 666, 3333, 33333, 55555, 66666, 33333, 3333, 6666, 11888, 26666, 38888, 56666, 42222, 39999, 28888, 17777, 9666, 6555, 5555, 3333, 2222, 3111, 6999, 5888, 2777, 1666, 999, 888, 777]
                    ],
//                    [
//                        'name' => 'UV',
//                        'type' => 'line',
//                        'smooth' => true,
//                        'itemStyle' => ['normal' => ['areaStyle' => ['type' => 'default']]],
//                        'data' => [33,33,323,33,33,11, 22, 33, 44, 55, 66, 333, 3333, 5555, 12666, 3333, 333, 666, 1188, 2666, 3888, 6666, 4222, 3999, 2888, 1777, 966, 655, 555, 333, 222, 311, 699, 588, 277, 166, 99, 88, 77]
//                    ]
                ]
            ],

            //访客浏览器分布
            [
                'title' => [
                    'text' => '访客浏览器分布',
                    'x' => 'center',
                    'textStyle' => [
                        'fontSize' => 14
                    ]
                ],
                'tooltip' => [
                    'trigger' => 'item',
                    'formatter' => "{a} <br/>{b} : {c} ({d}%)"
                ],
                'legend' => [
                    'orient' => 'vertical',
                    'x' => 'left',
                    'data' => []
                ],
                'series' => [
                    [
                        'name' => '访问来源',
                        'type' => 'pie',
                        'radius' => '55%',
                        'center' => ['50%', '50%'],
                        'data' => [
                        ]
                    ]
                ]
            ],

            //新增的用户量
            [
                'title' => [
                    'text' => '最近7天新增的用户量',
                    'x' => 'center',
                    'textStyle' => [
                        'fontSize' => 14
                    ]
                ],
                'tooltip' => [ //提示框
                    'trigger' => 'axis',
                    'formatter' => "{b}<br>新增用户：{c}"
                ],
                'xAxis' => [
                    [ //X轴
                        'type' => 'category',
                        'data' => ['11-07', '11-08', '11-09', '11-10', '11-11', '11-12', '11-13']
                    ]
                ],
                'yAxis' => [
                    [ //Y轴
                        'type' => 'value'
                    ]
                ],
                'series' => [
                    [ //内容
                        'type' => 'line',
                        'data' => [200, 300, 400, 610, 150, 270, 380]
                    ]
                ]
            ]
        ];
        $data[0]['xAxis'][0]['data'] = $time_jd;
        $data[0]['series'][0]['data'] = $time_count;
//        $data[0]['series'][1]['data'] = $time_count;
        $data[1]['series'][0]['data'] = $arr;
        $data[1]['legend']['data'] = $arr_s;
        if(empty($zc_time)||empty($zc_num)){
            $zc_empty_time = [
                date('m-d',strtotime("-6 day")),
                date('m-d',strtotime("-5 day")),
                date('m-d',strtotime("-4 day")),
                date('m-d',strtotime("-3 day")),
                date('m-d',strtotime("-2 day")),
                date('m-d',strtotime("-1 day")),
                date('m-d')
            ];
            $zc_empty_num = [
                0,0,0,0,0,0,0
            ];
            $data[2]['xAxis'][0]['data'] = $zc_empty_time;
            $data[2]['series'][0]['data'] = $zc_empty_num;
        }else{
            $data[2]['xAxis'][0]['data'] = $zc_time;
            $data[2]['series'][0]['data'] = $zc_num;
        }
        $res = json_decode(json_encode($data));
        return datasuccess($res);
    }
}
